package com.example.admin.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.admin.AdminActivity;
import com.example.admin.AdminLoginActivity;
import com.example.admin.R;
import com.example.admin.SQLite.User;
import com.example.admin.SQLite.dao.AdminDao;
import com.example.admin.Utils.GlideEngine;
import com.example.admin.base.BaseActivity;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.orhanobut.hawk.Hawk;

import java.util.List;

import static android.app.Activity.RESULT_OK;

public class Tab6Fragment extends Fragment {

    AdminDao adminDao;
    ImageView ivAvar;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_tab6, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        adminDao=new AdminDao(getActivity());
        ivAvar = view.findViewById(R.id.ivAvar);
        if (Hawk.contains("userAdmin" )) {
            imgPath = Hawk.get("userAdmin");
            Glide.with(this).load(imgPath).into(ivAvar);
        }
        view.findViewById(R.id.tvPAss).setOnClickListener(v -> {
            {

                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("请输入新密码");
                View views = getLayoutInflater().inflate(R.layout.dialog_updata_admin_pass, null);
                EditText input = views.findViewById(R.id.etPass);
                EditText inputs = views.findViewById(R.id.etrePass);
                builder.setView(views);
                builder.setPositiveButton("更改密码", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String str = input.getText().toString().trim();
                        String str1 = inputs.getText().toString().trim();
                        if (!TextUtils.equals(str, str1)) {
                            Toast.makeText(getActivity(), "密码不一致", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        String text = input.getText().toString();
                        adminDao.updateUserPass(1, text);
                        BaseActivity.finishAll();
                        startActivity(new Intent(getActivity(), AdminLoginActivity.class));

                    }
                });

// 显示对话框

                builder.show();
            }
        });
        view.findViewById(R.id.tvLoginOut).setOnClickListener(v -> {
            BaseActivity.finishAll();
            startActivity(new Intent(getActivity(), AdminLoginActivity.class));
        });

        view.findViewById(R.id.tvAvar).setOnClickListener(v -> {
            PictureSelector.create(Tab6Fragment.this)
                    .openGallery(PictureMimeType.ofImage())//全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
                    // .theme()//主题样式(不设置为默认样式) 也可参考demo values/styles下 例如：R.style.picture.white.style
                    .loadImageEngine(GlideEngine.createGlideEngine())
                    .maxSelectNum(1)// 最大图片选择数量 int
                    .minSelectNum(1)// 最小选择数量 int
                    .imageSpanCount(4)// 每行显示个数 int
                    .selectionMode(PictureConfig.SINGLE)// 多选 or 单选 PictureConfig.MULTIPLE or PictureConfig.SINGLE
                    .previewImage(true)// 是否可预览图片 true or false
                    .isCamera(true)// 是否显示拍照按钮 true or false
                    .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                    .sizeMultiplier(0.5f)// glide 加载图片大小 0~1之间 如设置 .glideOverride()无效
                    .enableCrop(true)// 是否裁剪 true or false
                    .compress(true)// 是否压缩 true or false
                    .hideBottomControls(true)// 是否显示uCrop工具栏，默认不显示 true or false
                    .freeStyleCropEnabled(true)// 裁剪框是否可拖拽 true or false
                    .openClickSound(true)// 是否开启点击声音 true or false
                    .previewEggs(true)// 预览图片时 是否增强左右滑动图片体验(图片滑动一半即可看到上一张是否选中) true or false
                    .minimumCompressSize(100)// 小于100kb的图片不压缩
                    .synOrAsy(true)//同步true或异步false 压缩 默认同步
                    .rotateEnabled(true) // 裁剪是否可旋转图片 true or false
                    .scaleEnabled(true)// 裁剪是否可放大缩小图片 true or false
                    .forResult(PictureConfig.CHOOSE_REQUEST);//结果回调onActivityResult code
        });
    }
    String imgPath;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        List<LocalMedia> images;
        if (resultCode == RESULT_OK) {
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {// 图片选择结果回调

                images = PictureSelector.obtainMultipleResult(data);

                if (images.get(0).isCompressed()) {
                    imgPath = images.get(0).getCompressPath();
                } else {
                    imgPath = images.get(0).getPath();
                }
                Glide.with(this).load(imgPath).into(ivAvar);
                Hawk.put("userAdmin" , imgPath);
            }
        }
    }
}